package com.szh.mysobproject.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.*
import com.szh.mysobproject.model.ArticleInfo
import com.szh.mysobproject.model.UserArticle
import com.szh.mysobproject.paging.source.ArticlePagingSource
import com.szh.mysobproject.paging.source.UserArticlePagingSource
import kotlinx.coroutines.flow.Flow


/**
 * @author : create by  szh
 * @date : 2022/7/8 9:10
 * @desc :文章列表的viewModel
 */

class ArticleViewModel : ViewModel() {

    /**
     * 指定用户的文章列表
     */
    fun getUserArticleList(userId: String): Flow<PagingData<UserArticle.UserArticleItem>> {
        return Pager(config = PagingConfig(30),
            pagingSourceFactory = {
                UserArticlePagingSource(userId)
            }).flow.cachedIn(viewModelScope)
    }

    /**
     * 根据分类获取文章列表
     */
    fun getArticleListByCategoryId(categoryId: String): Flow<PagingData<ArticleInfo.ArticleItem>> {
        return Pager(config = PagingConfig(30),
            pagingSourceFactory = { ArticlePagingSource(categoryId) })
            .flow.cachedIn(viewModelScope)
    }

}